package com.kakarote.authorization.controller;

import cn.hutool.core.util.StrUtil;
import com.kakarote.authorization.common.AuthorizationConst;
import com.kakarote.authorization.entity.AuthorizationUser;
import com.kakarote.authorization.entity.VO.CompanyDetails;
import com.kakarote.authorization.entity.VO.LoginVO;
import com.kakarote.authorization.service.AuthService;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.Const;
import com.kakarote.core.common.ParamAspect;
import com.kakarote.core.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author zhangzhiwei
 * 添加权限的controller
 */
@RestController
@Api(tags = "用户登录相关接口")
@Slf4j
public class AuthController {

    @Autowired
    private AuthService loginService;

    @RequestMapping(value = "/permission")
    @ParamAspect
    public Result<String> permission(@RequestParam("url") String url, @RequestParam("method") String method, HttpServletRequest request) {
        String token = request.getHeader(Const.TOKEN_NAME);
        String proxyHost = request.getHeader("proxyHost");
        return loginService.permission(token, url, proxyHost);
    }


    @RequestMapping("/getLoginQrCode")
    @ParamAspect
    @ApiOperation(tags = "获取手机端登录二维码", httpMethod = "POST", value = "getLoginQrCode")
    public Result<String> getLoginQrCode(HttpServletRequest request) {
        String loginQrCode = loginService.getLoginQrCode(request);
        return Result.ok(loginQrCode);
    }

    @RequestMapping("/getLoginQrInfo/{token}")
    @ParamAspect
    @ApiOperation(tags = "轮询获取登录信息", httpMethod = "POST", value = "getLoginQrCode")
    public Result<LoginVO> getLoginQrInfo(@PathVariable("token") String token) {
        LoginVO loginQrInfo = loginService.getLoginQrInfo(token);
        return Result.ok(loginQrInfo);
    }

    @RequestMapping("/setQrInfo/{token}")
    @ApiOperation(tags = "手机端设置信息", httpMethod = "POST", value = "getLoginQrCode")
    public Result<String> setQrInfo(@PathVariable("token") String token) {
        loginService.setQrInfo(token);
        return Result.ok();
    }

    /**
     * 登录方法，限流由sentinel处理
     */
    @PostMapping(value = "/login")
    @ApiOperation(tags = "用户登录", httpMethod = "POST", value = "/doLogin")
    @OperateLog(behavior = BehaviorEnum.LOGIN, apply = ApplyEnum.ADMIN, object = OperateObjectEnum.USER, type = OperateTypeEnum.LOGIN)
    public Result<?> doLogin(@Valid @RequestBody AuthorizationUser user) {
        return loginService.doLogin(user);
    }

    /**
     * 登录方法，限流由sentinel处理
     * 企业微信侧边栏
     */
    @PostMapping(value = "/wxLogin")
    @ApiOperation(tags = "企业微信侧边栏", httpMethod = "POST", value = "/wxLogin")
    public Result<?> wxLogin(@Valid @RequestBody AuthorizationUser user) {
        return loginService.wxLogin(user);
    }

    @RequestMapping(value = "/logout")
    @ApiOperation(tags = "用户注销", httpMethod = "GET", value = "/logout")
    public Result<String> logout(HttpServletRequest request) {
        String token = request.getHeader(Const.TOKEN_NAME);
        if (StrUtil.isNotEmpty(token)) {
            loginService.logout(token);
        }
        return Result.ok();
    }

    @PostMapping(value = "/reLogin")
    @ApiOperation(tags = "用户选择企业后登录", httpMethod = "POST", value = "/reLogin")
    public Result<?> reLogin(@RequestBody CompanyDetails companyDetails) {
        return loginService.reLogin(companyDetails);
    }

    /**
     * 单点鉴权
     */
    @RequestMapping(value = "/ssoAuth")
    @OperateLog(behavior = BehaviorEnum.SSO_AUTH, apply = ApplyEnum.ADMIN, object = OperateObjectEnum.USER, type = OperateTypeEnum.LOGIN)
    public Result<?> doSsoAuth() {
        log.info("开始单点鉴权PC");
        return loginService.doSsoAuth(AuthorizationConst.PC);
    }

    /**
     * 单点鉴权
     */
    @RequestMapping(value = "/ssoAuthApp")
    @OperateLog(behavior = BehaviorEnum.SSO_AUTH, apply = ApplyEnum.ADMIN, object = OperateObjectEnum.USER, type = OperateTypeEnum.LOGIN)
    public Result<?> doSsoAuthApp() {
        log.info("开始单点鉴权APP");
        return loginService.doSsoAuth(AuthorizationConst.APP);
    }

    /**
     * 单点登录
     */
    @RequestMapping(value = "/ssoLogin")
    @OperateLog(behavior = BehaviorEnum.SSO_LOGIN, apply = ApplyEnum.ADMIN, object = OperateObjectEnum.USER, type = OperateTypeEnum.LOGIN)
    public Result<?> doSsoLogin(@RequestParam(value = "code") String code, @RequestParam(value = "platform") String platform) {
        log.info("开始单点登录SSO");
        return loginService.doSsoLogin(code, platform);
    }
}
